package com.empty.oredermenu.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.util.TimeUtils
import android.view.View
import android.widget.AdapterView
import android.widget.GridView
import android.widget.ListView
import android.widget.Toast
import com.empty.oredermenu.R
import com.empty.oredermenu.adapter.FindListAdapter

import com.empty.oredermenu.base.OrderMenuBaseActivity
import com.empty.oredermenu.httpmannage.OkHttpMannager
import com.empty.oredermenu.httpmannage.OkHttp_CallResponse
import com.empty.oredermenu.javabeen.FindMenuListBeen
import com.empty.oredermenu.naviactivity.RestRouteShowActivity
import com.empty.oredermenu.utils.ToastUtils
import com.empty.oredermenu.utils.UrlAllUtils
import kotlinx.android.synthetic.main.activity_find_list.*
import okhttp3.Call
import okhttp3.Response
import org.json.JSONArray
import org.json.JSONObject
import uk.co.chrisjenx.calligraphy.CalligraphyConfig
import zhy.com.highlight.HighLight
import zhy.com.highlight.interfaces.HighLightInterface
import zhy.com.highlight.position.OnBottomPosCallback
import zhy.com.highlight.position.OnLeftPosCallback
import zhy.com.highlight.position.OnRightPosCallback
import zhy.com.highlight.position.OnTopPosCallback
import zhy.com.highlight.shape.CircleLightShape
import zhy.com.highlight.shape.RectLightShape


class MainActivity : OrderMenuBaseActivity(), OkHttp_CallResponse {

    var findMenuBeens : ArrayList<FindMenuListBeen> = ArrayList<FindMenuListBeen>()
    var findListAdapter : FindListAdapter? = null
    var list_find : GridView? = null
    var mHightLight :HighLight? = null


    override fun OkHttp_ResponseSuccse(call: Call?, response: Response?, jsonObject: JSONObject?, tag: String?) {
        Log.v("menufind","     请求成功         "+jsonObject.toString())
        if (jsonObject!!.getString("code").equals("10000")){
            if (jsonObject!!.getString("data").equals("null")){
                ToastUtils.Toast_Long(this,"未找到此分类")
                return
            }
            var datajson : JSONArray = jsonObject.getJSONArray("data")
            //kotlin 闭区间写法
            for (i in 0 until datajson.length()){
                var json : JSONObject = datajson.getJSONObject(i)
               var findbeen : FindMenuListBeen = FindMenuListBeen()
                findbeen.cookclass = json.getString("cookclass")
                findbeen.title = json.getString("title")
                findbeen.id = json.getString("id")
                findMenuBeens.add(findbeen)
            }
            findListAdapter!!.notifyDataSetChanged()
        }else{
            ToastUtils.Toast_Long(this,jsonObject!!.getString("message"))
        }
    }

    override fun OkHttp_ErrorCode(code: String?, tag: String?) {
        Log.v("menufind","     请求失败        "+code)
    }

    override fun user_NoLogin() {
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        toolsUtils!!.BuildColor(window, this, R.drawable.side_nav_bar)
        setContentView(R.layout.activity_find_list)



        baseInitView()
        layout_title!!.text = "菜肴分类"
        image_back!!.visibility = View.GONE

        list_find = findViewById(R.id.list_find) as GridView?

        findListAdapter = FindListAdapter(this@MainActivity,findMenuBeens)
        list_find?.adapter = findListAdapter

        val url: String = UrlAllUtils.URL_MENUFIND_ADDRESS +"?key="+ UrlAllUtils.URL_MENUFIND_KEY
        OkHttpMannager.getInstance().postAsynHttp(url,this@MainActivity,this,false,"find")

        list_find!!.setOnItemClickListener{ _, _, position, _ ->
            if (findMenuBeens.size>0){
                val intentmain:Intent = Intent();
                intentmain.setClass(this@MainActivity,MenuDetailListActivity::class.java)
                Log.v("thisorder","     findMenuBeens.get(position).id      "+findMenuBeens.get(position).id)
                intentmain.putExtra("menuid",findMenuBeens.get(position).id)
                startActivity(intentmain)
            }
        }

//        but_map.setOnClickListener(View.OnClickListener {
//            startActivity(Intent(this@MainActivity,AMapViewActivity::class.java))
//        })
//        but_mapDirl.setOnClickListener(View.OnClickListener {
//            startActivity(Intent(this@MainActivity, RestRouteShowActivity::class.java))
//        })
        text_menuorder.setOnClickListener{
            textOnSearch()
        }

        showTipView()
    }

    override fun OkHttp_CallonFailure(call: Call?) {

    }

    fun textOnSearch(){
        val intent = Intent()
        intent.setClass(this,EditMenuOrderActivity::class.java)
        startActivity(intent)
    }

    /**
     * 新手图层引导
     */
    fun showTipView() {

        mHightLight = HighLight(this@MainActivity)//
                .anchor(findViewById(R.id.id_container))//如果是Activity上增加引导层，不需要设置anchor
                .autoRemove(false)
                .enableNext()
                .setOnLayoutCallback {
                    //界面布局完成添加tipview
                    mHightLight!!.addHighLight(R.id.text_menuorder, R.layout.info_gravity_right_up, OnBottomPosCallback(), RectLightShape())
                    //然后显示高亮布局
                    mHightLight!!.show()
                }
                .setClickCallback {
                    mHightLight!!.next()
                }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mHightLight != null){
            mHightLight!!.remove()
        }

    }

}
